package com.wxj.shopping_mall.service.impl;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class thirdparty {

    private Logger logger = LoggerFactory.getLogger(thirdparty.class);

    private static final String url = "http//ddsd/dsd";

    /**
     * 带有参数的Post请求
     * NameValuePair
     */
    private String buildPost(String url, String passid) {
        // 创建http POST请求
        HttpPost httpPost = new HttpPost("url");
        StringEntity entity = new StringEntity(passid, "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
        return sendPost(httpPost);
    }

    private String sendPost(HttpPost httpPost) {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpClient.execute(httpPost);
            // 返回状态码是否是200
            if (response.getStatusLine().getStatusCode() != 200) {
                int code = response.getStatusLine().getStatusCode();
                logger.error("http status code:" + code);
                return String.valueOf(code);
            }
            String body = EntityUtils.toString(response.getEntity(), "UTF-8");
            // 使用实体类接收参数
//            UserVo userVo = new Gson().fromJson(body,UserVo.class);
            return body;
        } catch (IOException e) {
            logger.error("http error:" + e);
            return null;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (IOException e) {
                logger.error("http error:" + e);
            }
        }
    }

    // 版本2，使用 lambda 表达式
    private String sendPost2(HttpPost httpPost) {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            // 执行请求，lambda表达式
            return httpClient
                    .execute(httpPost, httpResponse -> {
                        if (httpResponse.getStatusLine().getStatusCode() != 200) {
                            int code = httpResponse.getStatusLine().getStatusCode();
                            logger.error("http status code:" + code);
                            return String.valueOf(code);
                        }
                        String body = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
                        //使用实体类接收参数
                        //UserVo userVo = new Gson().fromJson(body,UserVo.class);
                        return body;
                    });
        } catch (IOException e) {
            logger.error("http error:" + e);
            return null;
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                logger.error("http error:" + e);
            }
        }
    }

}
